﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WS.EKA.ServiceInterface;
using WS.EKA.DataAccess;
using WS.EKA.Model;
using WS.EKA.Utilities.Consts;

namespace WS.EKA.ServiceImpl
{
    public class PromotionService:IPromotion
    {
        public List<SalesPromotion> GetAllPromotion(decimal shopingCartPrice)
        {
            List<SalesPromotion> pList=new List<SalesPromotion>();
            DateTime time=DateTime.Now;
            var list = SalesPromotionContext.GetByConditions(
                t => t.IsUsed == 1 && t.IsDeleted == 0 && t.EndTime >= time && t.StartTime <= time
                , x => x.OrderBy(t => t.OrderID));
            foreach (var item in list)
            {
                bool result = true;
                if (item.PromotionType == string.Format("{0}.aspx", PromotionType.BuyAndGive_Operate.ToString()))
                { }
                else if (item.PromotionType == string.Format("{0}.aspx", PromotionType.FullAndDiscount_Operate.ToString()))
                {
                    var obj = FullAndDiscountContext.Get(t => t.SalesPromotionInfoGuid == item.Guid);
                    result = (shopingCartPrice >= obj.MinimalCost);
                }
                else if (item.PromotionType == string.Format("{0}.aspx", PromotionType.FullAndFavourTicket_Operate.ToString()))
                {
                    var obj = FullAndFavourTicketContext.Get(t => t.SalesPromotionInfoGuid == item.Guid);
                    result = (shopingCartPrice >= obj.MinimalCost);
                }
                else if (item.PromotionType == string.Format("{0}.aspx", PromotionType.FullAndFree_Operate.ToString()))
                {
                    var obj = FullAndFreeContext.Get(t => t.SalesPromotionInfoGuid == item.Guid);
                    result = (shopingCartPrice >= obj.MinimalCost);
                }
                else if (item.PromotionType == string.Format("{0}.aspx", PromotionType.FullAndGift_Operate.ToString()))
                {
                    var obj = FullAndGiftContext.Get(t => t.SalesPromotionInfoGuid == item.Guid);
                    result = (shopingCartPrice >= obj.Cost);
                }
                else if (item.PromotionType == string.Format("{0}.aspx", PromotionType.FullAndScore_Operate.ToString()))
                {
                    var obj = FullAndScoreContext.Get(t => t.SalesPromotionInfoGuid == item.Guid);
                    result = (shopingCartPrice >= obj.MinimalCost);
                }
                if(result)
                {
                    pList.Add(item);
                }
            }
            return pList;
        }

        public MemberFavourTicket VerifyFavourTicket(string loginId, string ticketCode)
        {
             DateTime time=DateTime.Now;
             var ticket = MemberFavourTicketContext.Get(t => t.MemLoginID == loginId && t.FavourTicketCode == ticketCode
                 && t.IsDeleted == 0 && t.EndDate <= time && t.StartDate >= time && t.LimitTimes>0);
             return ticket;
        }

        public List<MemberFavourTicket> GetFavourTicketByMember(string loginId)
        {
            DateTime time = DateTime.Now;
            var list = MemberFavourTicketContext.GetByConditions(t => t.MemLoginID == loginId 
                && t.IsDeleted == 0 && t.EndDate <= time && t.StartDate >= time && t.LimitTimes>0);
            return list ;
        }
    }
}
